package com.itheima.controller.impl;

import com.itheima.controller.IManagerController;
import com.itheima.controller.ISubwayController;
import com.itheima.domain.Subway;
import com.itheima.service.ISubwayService;
import com.itheima.service.impl.SubwayServiceImpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class SubwayControllerImpl implements ISubwayController {
    private Scanner sc = new Scanner(System.in);
    private ISubwayService service= new SubwayServiceImpl();
    @Override
    public void start() throws IOException {
        while (true){
            System.out.println("欢迎进入地铁管理系统");
            System.out.println("1 查看所有 2  新增车站  3退出 ");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    findAll();
                    break;
                case "2":
                    add();
                    break;
                case "3":
                    System.out.println("感谢你的使用");
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }

        }
    }

    @Override
    public void findAll() {
       ArrayList<Subway> list= service.findAll();
       if(list==null||list.size()==0){
           System.out.println("没有数据");
           return;
       }
        for (Subway subway : list) {
            System.out.println(subway);
        }
    }

    @Override
    public void add() throws IOException {

        String sid = null;
        while (true) {
            System.out.println("请输入编号");
            sid = sc.nextLine();
            boolean flag=service.exists(sid);
            if (flag){
                System.out.println("编号已经存在");
            }else {
                break;
            }
        }
        System.out.println("请输入名称");
        String name = sc.nextLine();
        System.out.println("请输入路线");
        String luxian = sc.nextLine();
        Subway subway = new Subway(sid, name, luxian);
        service.add(subway);
    }
}
